package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询营业数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        return BusinessDataVO.builder()
                .turnover(orderMapper.sumByMap(begin,end, Orders.COMPLETED))
                .validOrderCount(orderMapper.countStatus(Orders.COMPLETED))
                .orderCompletionRate(orderMapper.countStatus(Orders.COMPLETED).doubleValue()/ orderMapper.OrdercountByDate(begin, end, null))
                .unitPrice(orderMapper.sumByMap(begin,end,Orders.COMPLETED) / orderMapper.countStatus(Orders.COMPLETED))
                .newUsers(userMapper.countByDate(begin,end)).build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView(LocalDateTime begin, LocalDateTime end) {
        return OrderOverViewVO.builder().allOrders(orderMapper.OrdercountByDate( begin, end,null))
                .cancelledOrders(orderMapper.countStatus(Orders.CANCELLED))
                .deliveredOrders(orderMapper.countStatus(Orders.CONFIRMED))
                .waitingOrders(orderMapper.countStatus(Orders.TO_BE_CONFIRMED))
                .completedOrders(orderMapper.countStatus(Orders.COMPLETED))
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        return DishOverViewVO.builder()
                .sold(dishMapper.getDishOverView(null, StatusConstant.ENABLE))
                .discontinued(dishMapper.getDishOverView(null, StatusConstant.DISABLE))
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        return  SetmealOverViewVO.builder()
                 .sold(setmealMapper.getDishOverView(null, StatusConstant.ENABLE))
                 .discontinued(setmealMapper.getDishOverView(null, StatusConstant.DISABLE))
                 .build();
    }
}
